class Solution
{
public:
    bool canVisitAllRooms(vector<vector<int>> &rooms)
    {
        int n = rooms.size();
        unordered_set<int> visited;
        queue<int> q;

        visited.insert(0);
        q.push(0);

        while (!q.empty())
        {
            int room = q.front();
            q.pop();
            for (int key : rooms[room])
            {
                if (!visited.count(key))
                {
                    visited.insert(key);
                    q.push(key);
                }
            }
        }

        return visited.size() == n;
    }
};